package com.pengshun.modules.api.user.service.impl;

import com.pengshun.modules.api.coal.para.CoalBinessPara;
import com.pengshun.modules.api.coal.result.MerchantResult;
import com.pengshun.modules.api.coal.service.dto.MerchantDto;
import com.pengshun.modules.api.mapper.PsMerchantMapper;
import com.pengshun.modules.api.user.repository.UserAttentionRepository;
import com.pengshun.modules.api.user.service.UserAttentionService;
import com.pengshun.modules.api.user.service.dto.UserAttentionDto;
import com.pengshun.modules.api.user.service.mapstruct.UserAttentionTodo;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.domain.UserAttention;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
@RequiredArgsConstructor
public class UserAttentionServiceImpl implements UserAttentionService {

    private final UserAttentionRepository userAttentionRepository;
    private final UserAttentionTodo userAttentionTodo;
    private final PsMerchantMapper psMerchantMapper;
    private final MessageSource messageSource;

    @Override
    public BaseResult newAttention(UserAttentionDto para) {
        UserAttention attention=userAttentionRepository.findByMerchantIdAndUserId(para.getMerchantId(),para.getUserId());
        if(attention==null){
            para.setCreateTime(new Date());
            userAttentionRepository.save(userAttentionTodo.toEntity(para));
        }
        return new BaseResult(0,messageSource.getMessage("attention.success", null, Locale.CHINA));

    }

    @Override
    public BaseResult deleteAttention(UserAttentionDto para) {
        UserAttention attention=userAttentionRepository.findById(para.getUserAttentionId()).orElse(null);
        if(attention!=null){
            userAttentionRepository.deleteById(para.getUserAttentionId());
        }
        return new BaseResult(0,messageSource.getMessage("attention.canle", null, Locale.CHINA));
    }

    @Override
    public BaseResult getAttentionList(CoalBinessPara para) {
        int page = (para.getPage()) * para.getSize();
        List<MerchantResult> list =psMerchantMapper.getAttentionList(page,para.getSize(),para.getMerchantCategoryId(),para.getUserId(),para.getLngX(),para.getLatY());
        return  new BaseResult(0,list);
    }

    @Override
    public BaseResult selectBymerchantId(MerchantDto para) {
        UserAttention attention=userAttentionRepository.findByMerchantIdAndUserId(para.getMerchantId(),para.getUserId());
        attention=attention==null?new UserAttention():attention;
        return  new BaseResult(0,attention);
    }

}
